<?php
/*************************************************

用户公共控制器 - the
Author: Reload <17-10-23>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class VideoController extends CommonController {
    public function appraisal() {

            $val = I('get.val');
            if($val==2) {
                //国资系
                $map['background'] = 1;
            } elseif($val==3) {
                //风投系
                $map['background'] = 2;
            } elseif($val==4) {
                //银行系 
                $map['background'] = 3;
            } elseif($val==5) {
                //上市系
                $map['background'] = 4;
            } elseif($val==6) {
                //上市系
                $map['background'] = 5;
            }
			$map['cate_id'] = 3;
            $map['id'] = array("not in",'59,60');
            $video_app = M('VideoAppraisal')->where($map)->order('add_time desc')->select();
            if($video_app){
                foreach($video_app as $k => $v) {
					
                    $platform[$k]['add_time'] = date('Y-m-d',$v['add_time']);
                    $house_id= $v['house_id'];
                    $map = array();
                    $map['id'] = $house_id;
                    $video_app[$k]['video_info'] = D('Video')->get_one($map);
                    
                    if(!$house_id){
                        $map = array();
                        $map['id'] = $v['news_id'];
                        $video_app[$k]['news_info'] = D('NewsHouse')->get_one($map,$field="pic");
                        $video_app[$k]['house_id'] =0;
                       // echo D('NewsHouse')->_sql();
                    }
					if($v['house_id'] == 574){
						// unset($video_app[$k]);
                        $map = array();
                        $map['id'] = $v['news_id'];
                        $video_app[$k]['news_info'] = D('NewsHouse')->get_one($map,$field="pic");
                        $video_app[$k]['house_id'] =0;               // 
					}

                }
            } 
                
            //   print_r($video_app);
            $this->assign('video_app',$video_app);

        $this->display();
    }


    //视频详情页
    public function appraisal_info() {
        $cate_id = I('get.cate_id');
        $p_id = I('get.p_id');
        $v_id = I('get.v_id');
        if($p_id){
            $map['id'] = $p_id;
            $platform = D('Platform')->get_one($map);
            $map = array();
            $map['platform_id'] = $p_id;
            $map['cate_id'] = $cate_id;
            if($v_id){
                $map['house_id'] = $v_id;
            }else{
                $map['house_id'] = array('gt',0);
            }
            $video_app = M('VideoAppraisal')->where($map)->find();
            $map = array();
            $map['id'] = $video_app['house_id'];
            $video = D('Video')->get_one($map);
            $map = array();
            $map['id'] = $video_app['news_id'];
            $news = D('NewsHouse')->get_one($map);
            $map = array();
            $map['platform_id'] = $p_id;
            $map['cate_id'] = $cate_id;
            $videodata = M('VideoAppraisal')->where($map)->select();
        //    print_r($videodata);
            $map = array();
            foreach($videodata as $k => $v) {
                $map['jg_video.id'] = $v['house_id'];
                $field = 'jg_video.id,jg_video.video_id,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.play_amount';
                $v_info = D('Video')->jion_play_one($map,$field);
                $v_info['play_amount_z'] = $v_info['push_play']+$v_info['play_amount'];
                $videodata[$k]['video'] = $v_info;
            }
        }else{
            $id = I('get.a_id');
            $map = array();
            $map['id'] = $id;
            $video_app = M('VideoAppraisal')->where($map)->find();
            $platform_name = $video_app['platform_name'];

            $map = array();
            $map['cate_id'] = $cate_id;
            $map['platform_name'] = $platform_name;
            $video_app = M('VideoAppraisal')->where($map)->find();
             $map = array();
            $map['id'] = $video_app['house_id'];
            $video = D('Video')->get_one($map);
            $map = array();
            $map['id'] = $video_app['news_id'];
            $news = D('NewsHouse')->get_one($map);
        }
        
    //   print_r($video);
        $videodata_count = count($videodata);
        $this->assign('videodata_count',$videodata_count);
        $this->assign('videodata',$videodata);
        $this->assign('video',$video);
        $this->assign('news',$news);
        $this->assign('plat',$platform);

        $this->display('Video/appraisal_info');

    }


    //播放量统计  -》记录播放历史
    public function play_count(){
        $id = I('POST.id');
        $info_id = I('POST.info_id');
        $user_id = $_SESSION['user']['id'];
        // echo $info_id;die;
        if($user_id && $info_id){

            $map['user_id']=$user_id;
            $map['video_id']=$info_id;
            $r = M('PlayRecord')->where($map)->find();
            if(!$r){
                $data['user_id'] = $user_id;
                $data['video_id'] = $info_id;
                $data['add_time'] = time();
                $res = M('PlayRecord')->data($data)->add();
            }else{
                $play_id = $r['id'];
                $data = array();
                $data['add_time'] = time();
                $map = array();
                $map['id'] = $play_id;
                $res = M('PlayRecord')->where($map)->save($data);
            }

        }
        $map['id'] = $id;
        $res = D('VideoHouse')->set_read_int($map);
        $this->ajaxReturn($res);
    }


    //栏目页
    public function program(){
        //个人信息
        $user_id = $_SESSION['user']['id'];
        $map['id'] = $user_id;
        $user = D('User')->get_one($map);
        if($user){
            //根据身份证号，自动返回性别  
            $sexint = (int)substr($user['idcard'],16,1);  
            if($sexint % 2 === 0){
                $user['sex']="女士";
            }else{
                $user['sex']="先生";

            }  

            if($user['realname']){
                $user['firstname'] =mb_substr($user['realname'],0,1,'utf8').$user['sex'];  
            }
            
        }
        
          //计算当前时间
        $now = time();
        $today = date('H',time());
        if($today>=6 and $today<=12){
            $now_time = "早上";
        }else if($today>12 and $today<=14){
            $now_time = "中午";
        }else if($today>14 and $today<18){
            $now_time = "下午";
        }else{
            $now_time = "晚上";
        }

        $id = I('get.id');
        $map = array();
        $program = $id;
        $order = 'jg_video.sort desc';
        $map['jg_video.program'] = $program;
        $map['jg_video.status'] = 0;
        $field = 'jg_video.id,jg_video.video_id,jg_video.wx_pic,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.host,jg_video_house.play_amount';
        $list = $this->program_get_video($map,$order,$field,0,9);
        foreach($list as $k=>$v){
            $map = array();
            $map['id'] = $v['host'];
            $host =  M('Host')->where($map)->field('name')->find();
            $list[$k]['host_name'] =$host['name'];
             $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $list[$k]['content_short'] =$news['content_short'];
			    $list[$k]['play_count'] =$v['push_play']+$v['play_amount'];
            $list[$k]['add_date'] =date('y-m-d',$v['add_time']);

        }
		//print_r($list);
        $this->assign('user',$user);
        $this->assign('now_time',$now_time);
        $this->assign('list',$list);
        $this->display();
    }

    public function ajax_get_program(){

        $id = I('get.id');
        $p = I('post.p');
        if($p==1){
            $page = $p*8+1;
        }else{
            $page = $p*8;
        }
        $map = array();
        $program = $id;
        $order = 'jg_video.sort desc';
        $map['jg_video.program'] = $program;
        $map['jg_video.status'] = 0;
		$field = 'jg_video.id,jg_video.video_id,jg_video.wx_pic,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.play_amount';
        $list = $this->program_get_video($map,$order,$field,$page,8);
        foreach($list as $k=>$v){
            $map = array();
            $map['id'] = $v['host'];
            $host =  M('Host')->where($map)->field('name')->find();
            $list[$k]['host_name'] =$host['name'];
            
             $map = array();
            $map['id'] = $news_id;
            $news = M('NewsHouse')->where($map)->field('content_short')->find();
            $list[$k]['content_short'] =$news['content_short']?$news['content_short']:'';
            $list[$k]['title'] =msubstr($v['title'],0,23);
			    $list[$k]['play_count'] =$v['push_play']+$v['play_amount'];
            $list[$k]['add_date'] =date('Y-m-d',$v['add_time']);

        }
        // echo $page;
        // print_r($list);
        $this->ajaxReturn($list);
    }
    public function program_get_video($map,$order,$field,$start,$end){
        $list = D('video')->jion_play($map,$order,$field,$start,$end);
        
        foreach ($list as $k => $v) {
            $list[$k]['play'] = $v['play_amount']+$v['push_play'];
        }
        return $list;
    }

    /**
     * [video_info 视频详情]
     * @return [type] [description]
     */
    public function video_info(){
        $id = I('get.id');
        $map['id'] = $id;
        $video_info = D('Video')->get_one($map);
        $video_id = $video_info['video_id'];
        $news_id = $video_info['news_id'];
        $map = array();
        $map['id'] =$video_id;
        $where['id'] =$news_id;
         
        $video_info['video_info'] = D('VideoHouse')->get_one($map);//视频详情
        $video_info['news_info'] = D('NewsHouse')->get_one($where); //新闻详情
        //节目名 主持人
        if($video_info['program']){
            $video_info['program_name'] =get_program_name($video_info['program']);
        }
        $host_id = explode(',', $video_info['host']);
        foreach ($host_id as $k => $v) {
            $host_arr[$k]['host_name'] =get_host_name($v);
            $host_arr[$k]['host_pic']=get_host_pic($v);
        }
        $video_info['host_arr'] = $host_arr;

        //标签
        $video_info['cate_name'] =get_cate_name($video_info['category']);
        $lable = explode(',',$video_info['label_id']);
        foreach($lable as $k=>$v){
            $video_info['lable_name'][]=get_lable_name($v);
        }
        $info  = $video_info;
         //播放列表
        $type = I('get.type');
        $label_id = I('get.label_id');
        if($type==1){
            //按分类查询列表
            $cate_id = $info['category'];
            $cate_name = get_cate_name($cate_id);
            $play_list = $this-> get_play_list($cate_id,$program=0,$label_id=0);
            $this->assign('menu_name',$cate_name);
            $this->assign('menu_url',0);
        }else if($type==2){
            //按节目查询列表
            $program = $info['program'];
            $program_name = get_program_name($program);
            $play_list = $this-> get_play_list($cate_id=0,$program,$label_id=0);
            $this->assign('menu_name',$program_name);
            $this->assign('program',$program);
            $this->assign('menu_url',1);

        }else if($type==3){
            //按分类下标签查询列表
            $cate_id = $info['category'];
            $cate_id = $info['category'];
            $cate_name = get_cate_name($cate_id);
            $play_list = $this-> get_play_list($cate_id,$program=0,$label_id);
            $this->assign('menu_name',$cate_name);
            $this->assign('menu_url',0);
        }
        foreach($play_list as $k=>$v){
            $play_list[$k]['play_count'] = $v['play_amount'] + $v['push_play'];
            
        }

         foreach($play_list as $k=>$v){
            $play_list[$k]['program_name'] =get_program_name($v['program']);
             if($v['id'] == $info['id']){
                 unset($play_list[$k]);
				 $new_tab = $v;
             }
             
        }
     //    array_unshift($play_list,$new_tab);
// print_r($play_list);die;
         //获取评论数据
        $page = 0;
        $pagenum = 5;
        $id_target = $id;
        $comments = R('Comment/get_comments',array($id_target,$page,$pagenum,$type=2));
        // print_r($comments);
        $map = array();
        $map['id_target'] =$id;
        // $map['id_parent'] =0;
        $map['type'] =2;
        $count = D('Comment')->count($map);
        $comment_count = ceil($count/$pagenum);
        //print_r($comments);die;
        $this->assign('play_list',$play_list);
        $this->assign('info',$info);
        $this->assign('comments',$comments);
        $this->display();
    }

    /**
     * [get_comments_info 单条评论详情]
     * @return [type] [description]
     */
    public function get_comments_info(){
        $id = I('post.id');
        $comments = R('Comment/get_comments',array(0,0,1,$type=2,$id));
        // print_r($comments);die;
        $this->assign('comments',$comments[0]);
        $content = $this->fetch('comments_info_item');
        $this->ajaxReturn(array('content'=>$content));

    }

        //获取跟多评论
    public function get_more_comment(){
        $id_target = I('post.id_target');
        $pagenum = I('post.pagenum');
        $map['id_target'] = $id_target;
        $count = D('Comment')->count($map);
            //总页数
        $num = 5;
        $count_page = ceil($count/$num);
        $page = ($pagenum-1)*$num;
        $pagenum = ($pagenum)*$num;

        $comments = R('Comment/get_comments',array($id_target,$page,$pagenum,$type=2));
        $this->assign('count_page',$count_page);
        $this->assign('comments',$comments);
        $content = $this->fetch('Video/comment_list_item');
        $r = array("content" =>$content,"count"=>0,"page"=>$page,"count"=>$count_page,'a'=>$count);
        $this->ajaxReturn($r);


    }


    /**
     * [get_play_list 获取播放列表]
     * @param  integer $cate_id  [分类ID]
     * @param  integer $program  [节目id]
     * @param  integer $label_id [标签ID]
     * @return [type]            [array]
     */
    public function get_play_list($cate_id=0,$program=0,$label_id=0){
        //$cate_id  =>列表为改分类下所有视频
        //$program  =>列表为改节目下所有视频
        //$label_id =>列表为该分类下所有含有该标签属性的视频
        $field = 'jg_video.id,jg_video.wx_pic,jg_video.video_id,jg_video.news_id,jg_video.title,jg_video.category,jg_video.pic,jg_video.hd_url,jg_video.program,jg_video.count_comments,jg_video.add_time,jg_video.label_id,jg_video_house.push_play,jg_video_house.play_amount';
        if($cate_id && !$label_id){
            $category = $cate_id;
            $order = 'jg_video.sort desc';
            $map['jg_video.category'] = $category;
            $list = $this->cate_get_video($map,$order,$field,0,10);
            return $list;
        }

        if($cate_id && $label_id){
            $category = $cate_id;
            $order = 'jg_video.sort desc';
            $map['jg_video.category'] = $category;
            $list = $this->cate_get_video($map,$order,$field,0,10);

             foreach($list as $k=>$v){
                $label_arr = explode(',',$v['label_id']);

                 if(in_array($label_id, $label_arr)){
                     $arr[] = $v;
                 }
            }
             return $arr;
        }

        if($program){
            $program = $program;
            $order = 'jg_video.sort desc';
            $map['jg_video.program'] = $program;
            $list = $this->cate_get_video($map,$order,$field,0,10);
            return $list;
        }

    }


    /**
     * [cate_get_video 分类获取数据]
     * @param  [type] $map   [条件]
     * @param  [type] $order [排序]
     * @param  [type] $field [字段]
     * @param  [type] $start [起始查询]
     * @param  [type] $end   [查询条数]
     * @return [type]        [array]
     */
    public function cate_get_video($map,$order,$field,$start,$end){

        $list = D('video')->jion_play($map,$order,$field,$start,$end);
        //return D('video')->_sql();
        foreach ($list as $k => $v) {
            $list[$k]['play'] = $v['play_amount']+$v['push_play'];
             $news_id = $v['news_id'];
            if($news_id){
                $map=array();
                $map['id'] = $news_id;
                $news_info = D('NewsHouse')->get_one($map);
                $list[$k]['news_content_short'] = $news_info['content_short'];
            }
        }
        return $list;
    }



}